/****** Object:  Stored Procedure dbo.spr_TourIS_Search  ******/

 use [TourIS]
 GO

 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spr_TourIS_Search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 drop procedure [dbo].[spr_TourIS_Search]
 GO

 SET QUOTED_IDENTIFIER OFF
 GO
 SET ANSI_NULLS OFF
 GO

 CREATE  PROCEDURE [dbo].[spr_TourIS_Search]
(
	@table varchar(60), -- table name
	@sort_by_col varchar(60)=null,
	@col_1 varchar(60)=null, -- column name
	@val_1 varchar(60)=null, -- value for @col_1
	@col_2 varchar(60)=null, -- column name
	@val_2 varchar(60)=null, -- value for @col_2
	@col_3 varchar(60)=null, -- column name
	@val_3 varchar(60)=null -- value for @col_3
	)
 AS

BEGIN
	SET NOCOUNT ON
	DECLARE @Err int

	DECLARE @SQLString NVARCHAR(1000);
	SET @SQLString = N'SELECT * ' + CHAR(13);
	SET @SQLString = @SQLString	+ ' FROM ' + @table + ' ' + CHAR(13);
	IF(@col_1 IS NOT NULL)
	BEGIN
		SET @SQLString = @SQLString	+ ' WHERE ' + CHAR(13);
		SET @SQLString = @SQLString	+ @col_1 + ' LIKE ''%' +  @val_1 + '%''' + CHAR(13);
	END
	IF(@col_2 IS NOT NULL)
	BEGIN
		SET @SQLString = @SQLString	+ ' AND ' + CHAR(13);
		SET @SQLString = @SQLString	+ @col_2 + ' LIKE ''%' +  @val_2 + '%''' + CHAR(13);
	END
	IF(@col_3 IS NOT NULL)
	BEGIN
		SET @SQLString = @SQLString	+ ' AND ' + CHAR(13);
		SET @SQLString = @SQLString	+ @col_3 + ' LIKE ''%' +  @val_3 + '%''' + CHAR(13);
	END
	IF(@sort_by_col IS NOT NULL)
	BEGIN
	SET @SQLString = @SQLString	+ ' ORDER BY ' + @sort_by_col + CHAR(13);
	END
	--print @SQLString
	EXEC sp_executesql @SQLString;

	SET @Err = @@Error
	RETURN @Err
 END

 GO

 SET QUOTED_IDENTIFIER OFF
 GO
 SET ANSI_NULLS ON
 GO

